package all.com.jinyongqunxiax.modules;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.randy.fundation.JxFunc;
import com.randy.fundation.Logger;

import all.com.jinyongqunxiax.R;
import all.com.jinyongqunxiax.component.JxForm;
import all.com.jinyongqunxiax.component.JxFormHeader;
import all.com.jinyongqunxiax.component.JxFormItemText;
import all.com.jinyongqunxiax.databinding.MapDetailActivityBinding;
import all.com.jinyongqunxiax.model.mod.MapManager;
import all.com.jinyongqunxiax.model.mod.manager.Mod;
import all.com.jinyongqunxiax.model.mod.manager.ModManager;
import all.com.jinyongqunxiax.view.JxHud;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import jy.game.components.map.JyMap;

public class MapDetailActivity extends AppCompatActivity {
    private Options                  options;
    private MapDetailActivityBinding binding;
    JxForm form = new JxForm();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        options = new Options(getIntent());

        setTitle(options.item);

        Mod        mod     = ModManager.shared().get(options.mod);
        MapManager manager = mod.mapManager;
        JyMap      model   = manager.map.get(options.item);

        binding = DataBindingUtil.setContentView(this, R.layout.map_detail_activity);

        RecyclerView recyclerView = binding.recyclerView;
        form.connect(this, recyclerView);

        if (model != null) {
            form.add(new JxFormHeader(0, "音乐", null));
            JxFunc.of(model.getMusics()).ifPresent(musics -> {
                JxFunc.of(musics).ifPresent(music -> {
                    form.add(new JxFormItemText(1, JxForm.TextField.Builder.build().label("音乐").value(music.name).hint("").value(), null));
                });
            });

            form.add(new JxFormHeader(0, "元素", null));

            JxFunc.of(model.mapUnits).ifPresent(unit -> {
                form.add(new JxFormHeader(1, unit.name, null));
                form.add(new JxFormItemText(2, JxForm.TextField.Builder.build().label("desc").value(unit.description).hint("").value(), null));

                form.add(new JxFormItemText(2, JxForm.TextField.Builder.build().label("x").value("" + unit.x).hint("").value(), null));
                form.add(new JxFormItemText(2, JxForm.TextField.Builder.build().label("y").value("" + unit.y).hint("").value(), null));
                JxFunc.of(unit.events).ifPresent(event -> {
                    form.add(new JxFormHeader(2, event.type + " " + event.value, null));
                    form.add(new JxFormItemText(3, JxForm.TextField.Builder.build().label("几率").value("" + event.probabilityValue).hint("").value(), null));
                    form.add(new JxFormItemText(3, JxForm.TextField.Builder.build().label("重复").value(event.repeatValue).hint("").value(), null));

                    form.add(new JxFormHeader(3, "触发条件", null));
                    JxFunc.of(event.conditions).ifPresent(condition -> form.add(new JxFormItemText(4, JxForm.TextField.Builder.build().label(condition.type).value(condition.value).hint("").value(), null)));
                });
            });
        }
        form.refresh();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.plus_button_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_add_button:
                JxHud.info("add menu clicked");
                break;
            default:
                Logger.w("not handle menu %d", item.getItemId());
                break;
        }
        return true;
    }

    public static class Options {
        String mod;
        String item;

        Options(Intent intent) {
            this.mod = intent.getStringExtra("mod");
            this.item = intent.getStringExtra("item");
        }

        static Intent intent(Context context, String mod, String item) {
            Intent intent = new Intent(context, MapDetailActivity.class);
            intent.putExtra("mod", mod);
            intent.putExtra("item", item);
            return intent;
        }
    }
}
